package com.xbh.captcha.controller;

import com.xbh.captcha.service.CaptchaSaveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @program: captcha-provider
 * @description:
 * @author: 许宝华
 * @create: 2021-09-19 12:16
 */


@RestController
@RequestMapping(path = "/captcha")
public class CaptchaController {

    @Autowired
    private CaptchaSaveService captchaSaveService;

    @GetMapping("/save")
    public String captcha(HttpServletRequest request, HttpServletResponse response) throws Exception{

        // 设置请求头为输出图片类型
        response.setContentType("image/gif");
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        String code = captchaSaveService.captchaSave(request, response);
        return code;
    }

    @GetMapping("/savePng")
    public String captchaPng(HttpServletRequest request, HttpServletResponse response) throws Exception{

        // 设置请求头为输出图片类型
        response.setContentType("image/gif");
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        String code = captchaSaveService.captchaSavePng(request, response);
        return code;
    }
}
